package com.yqRecite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqRecite.entity.Words;
import com.yqRecite.mapper.WordsMapper;
import com.yqRecite.service.WordsService;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Service
public class WordsServiceImpl implements WordsService {
    @Resource
    private WordsMapper wordsMapper;

    /**
     * 增加一个单词或句子
     * @param words
     * @return
     */
    @Override
    public Result addOneWord(Words words) {
        int res = wordsMapper.insert(words);
        if (res == 1){
            return ResultUtil.ok(words);
        }else {
            return ResultUtil.fail("增加失败", words);
        }
    }

    /**
     * 获取所有单词
     * @return
     */
    @Override
    public Result getAllWordsList() {
        QueryWrapper<Words> wrapper = new QueryWrapper<>();
        List<Words> wordsList = wordsMapper.selectList(wrapper);
        if (wordsList == null || wordsList.size() == 0){
            System.out.println("数据为空！");
        }else {
            System.out.println("单词数据获取成功！");
        }
        return ResultUtil.ok(wordsList);
    }

    /**
     * 在指定用户的单词里查找匹配类型的单词
     * @param type 类型
     * @param uid 用户id
     * @return
     */
    @Override
    public Result getMyWordsListByType(int uid, int type) {
        QueryWrapper<Words> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid).eq("type", type);
        List<Words> wordsList = wordsMapper.selectList(wrapper);
        if (wordsList == null || wordsList.size() == 0){
            System.out.println("数据为空！");
        }else {
            System.out.println("单词数据获取成功！");
        }
        return ResultUtil.ok(wordsList);
    }

    /***
     * 根据类型获取单词（收藏、喜欢...）
     * @param type
     * @return
     */
    @Override
    public Result getAllWordsListByType(int type) {
        QueryWrapper<Words> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        List<Words> wordsList = wordsMapper.selectList(wrapper);
        if (wordsList == null || wordsList.size() == 0){
            System.out.println("数据为空！");
        }else {
            System.out.println("单词数据获取成功！");
        }
        return ResultUtil.ok(wordsList);
    }



    /**
     * 删除一个单词，这里用于取消收藏，取消喜欢等等
     * @param id
     * @return
     */
    @Override
    public Result deleteWords(int id) {
        Words words = wordsMapper.selectById(id);
        if (words == null){
            return ResultUtil.error("数据不存在");
        }else {
            int res = wordsMapper.deleteById(id);
            if (res == 1){
                System.out.println("成功！");
                return ResultUtil.ok();
            }else {
                System.out.println("单词删除失败！");
                return ResultUtil.fail("失败");
            }
        }
    }

    /**
     * 获取我的收藏，我的喜欢，我的生词本
     * @param uid
     * @return
     */
    @Override
    public Result getCollectNewNoteLove(int uid) {
        QueryWrapper<Words> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("type", 1);
        wrapper1.eq("uid", uid);
        List<Words> myCollectList = wordsMapper.selectList(wrapper1);//我的收藏
        QueryWrapper<Words> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("type", 2);
        wrapper2.eq("uid", uid);
        List<Words> myNewNoteList = wordsMapper.selectList(wrapper2);//我的生词本
        QueryWrapper<Words> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("type", 3);
        wrapper3.eq("uid", uid);
        List<Words> myLoveList = wordsMapper.selectList(wrapper3);//我的喜欢
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("myCollectList", myCollectList);
        returnMap.put("myNewNoteList", myNewNoteList);
        returnMap.put("myLoveList", myLoveList);
        return ResultUtil.ok(returnMap);
    }


    /**
     * 操作单词，加入和取消（收藏，生词本，喜欢）
     * @param uid 用户id
     * @param type 类型（1、收藏，2、生词本，3、喜欢）
     * @param words 单词数据
     * @return
     */
    @Override
    public Result operatorWord(int uid, int type, Words words) {
        //先判断单词是否存在，来判定是取消还是加入
        QueryWrapper<Words> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid).eq("type", type);
        List<Words> wordsList = wordsMapper.selectList(wrapper);
        boolean isExist = false;
        int id = 0;
        for (Words item: wordsList) {
            if (item.getWord().toLowerCase().equals(words.getWord().toLowerCase())){
                isExist = true;//存在
                id = item.getId();
                break;
            }
        }
        System.out.println("isExist==>"+isExist);
        if (!isExist){//不存在则加入
            words.setType(type);
            words.setUid(uid);
            int res = wordsMapper.insert(words);
            if (res == 1){
                return ResultUtil.ok("加入成功");
            }else {
                return ResultUtil.fail("加入失败", words);
            }
        }else {//存在则删除
            int res = wordsMapper.deleteById(id);
            if (res == 1){
                return ResultUtil.ok("操作成功");
            }else {
                return ResultUtil.fail("操作失败", words);
            }
        }
    }
}
